//
//  AddAddressViewController.m
//  HYApp
//
//  Created by Jianbo Cai on 2020/9/2.
//  Copyright © 2020 Unidax. All rights reserved.
//

#import "AddAddressViewController.h"
#import "SheetView.h"
#import "HYNetHelper.h"
#import "ScanQRCodeViewController.h"
#import "ChangeVerifyView.h"

@interface AddAddressViewController ()

@property (weak, nonatomic) IBOutlet UITextField *assetsTextField;
@property (weak, nonatomic) IBOutlet UITextField *addressTextField;
@property (weak, nonatomic) IBOutlet UITextField *remarkTextField;

@property (weak, nonatomic) IBOutlet UIButton *confirmButton;
@property (weak, nonatomic) IBOutlet UILabel *assetsTitleLable;
@property (weak, nonatomic) IBOutlet UILabel *addressTitleLable;
@property (weak, nonatomic) IBOutlet UILabel *remakeTitleLable;

@end

@implementation AddAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = JFMainLocalizedString(@"myProfile.AddAdress");//@"ADD Address";
    
    [self.confirmButton setBackgroundImage:[UIImage imageWithColor:UIColorFromHex(0xCCCCCC) size:self.confirmButton.frame.size] forState:UIControlStateDisabled];
    [self.confirmButton setBackgroundImage:[UIImage imageWithColor:UIColorFromHex(0x70896A) size:self.confirmButton.frame.size] forState:UIControlStateNormal];
    self.confirmButton.enabled = NO;
    
    
    self.assetsTitleLable.text = JFMainLocalizedString(@"myAssets.title");
    self.assetsTextField.AWplaceholder(JFMainLocalizedString(@"myAssets.title"));
    
    
    self.addressTitleLable.text = JFMainLocalizedString(@"myProfile.Address");
    self.addressTextField.AWplaceholder(JFMainLocalizedString(@"myProfile.EnterAddress"));
    
    self.remakeTitleLable.text = JFMainLocalizedString(@"myProfile.remake");
    self.remarkTextField.AWplaceholder(JFMainLocalizedString(@"myProfile.remake"));
    
    self.confirmButton.AWtitle(JFMainLocalizedString(@"confirm"), UIControlStateNormal);
    
    if (self.cryptoAddress) {
        self.remarkTextField.text = self.cryptoAddress.remarks;
        self.addressTextField.text = self.cryptoAddress.wallet_address;
        self.assetsTextField.text = self.cryptoAddress.currencies_id.chain_type;
    }
}

- (void)requestWithToken:(NSString *)token {
    NSString *currencies_id ;
    for (Currencie *entity in Currencies().currencieList) {
        if ([entity.name isEqualToString:self.assetsTextField.text]) {
            currencies_id = entity.ID;
            break;
        }
    }
    
    WEAKSELF
    if (self.cryptoAddress.ID) {
        [HYNetHelper userCryptoAddressDeleteWithId:self.cryptoAddress.ID callback:^(JFResultVo *resultVo) {
            if (!resultVo.errorNo) {
            }
        }];
    }
    
    [HYNetHelper userCryptoAddress_new:self.remarkTextField.text wallet_address:self.addressTextField.text currencies_id:currencies_id token:token callback:^(JFResultVo *resultVo) {
        if (!resultVo.errorNo) {
            [SLRemindView showMessage:@"添加成功" toView:weakSelf.view];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }else {
            NSString *messsage;
            if (resultVo.results[@"data"]) {
                messsage = resultVo.results[@"data"][@"message"];
            }
            FZAlertController *alertController = [FZAlertController alertControllerWithTitleImage:[UIImage imageNamed:@"alert_icon_failed"] title:@"Requested faild" customView:nil message:messsage];
            alertController.showCloseBtn = YES;
            alertController.messageAlignment = NSTextAlignmentCenter;
            
            FZAlertAction *action = [FZAlertAction actionWithTitle:JFMainLocalizedString(@"myProfile.ok") style:UIAlertActionStyleDefault handler:^(FZAlertAction *action) {
                                }];
            [alertController addAction:action];
            [self presentViewController:alertController animated:YES completion:nil];
        }
    }];
}

- (IBAction)selectAssetAction:(id)sender {
    NSArray *array = @[@"BTC", @"ETH", @"USDT"];
    
    SheetView *view = [[[NSBundle mainBundle] loadNibNamed:@"SheetView" owner:nil options:nil] firstObject];
    view.frame = self.navigationController.view.bounds;
    view.titleArray = array;
    WEAKSELF
    view.selectAction = ^(NSInteger index) {
        weakSelf.assetsTextField.text = array[index];
    };
    [self.navigationController.view addSubview:view];
}

- (IBAction)editChangeAction:(id)sender {
    if ((self.assetsTextField.text.length > 0) && (self.addressTextField.text.length > 0) && (self.remarkTextField.text.length > 0)) {
        self.confirmButton.enabled = YES;
    }else {
        self.confirmButton.enabled = NO;
    }
}

- (IBAction)confirmAction:(id)sender {
    ChangeVerifyView *view = [[[NSBundle mainBundle] loadNibNamed:@"ChangeVerifyView" owner:nil options:nil] firstObject];
    view.frame = CGRectMake(0, 0, CUSTOMVIEW_WIDTH, 110);
    FZAlertController *vc = [FZAlertController alertControllerWithTitleImage:[UIImage imageNamed:@"Captcha_ico_b"] title:nil customView:view message:nil];
    vc.showCloseBtn = YES;
    WEAKSELF
    FZAlertAction *action = [FZAlertAction actionWithTitle:JFMainLocalizedString(@"confirm") style:UIAlertActionStyleDefault handler:^(FZAlertAction *action) {
        [weakSelf requestWithToken:view.codeText.text];
                        }];
    [vc addAction:action];
    [self presentViewController:vc animated:YES completion:nil];
}

- (IBAction)scanAction:(id)sender {
    ScanQRCodeViewController *vc = [[ScanQRCodeViewController alloc]init];
    WEAKSELF
    vc.scanCode = ^(NSString * _Nonnull codeString) {
        weakSelf.addressTextField.text = codeString;
    };
    [self.navigationController pushViewController:vc animated:YES];
}

@end
